package com.xiaoxi.bms.controller.role;

import com.baomidou.mybatisplus.core.metadata.IPage;


import com.xiaoxi.bms.common.constant.constant.OperationConstants;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.user.RoleUserEnums;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.domain.role.BMSRole;
import com.xiaoxi.bms.domain.role.dto.BMSRoleDTO;
import com.xiaoxi.bms.domain.role.vo.BMSRoleVO;
import com.xiaoxi.bms.service.common.aspect.operation.OperationLog;
import com.xiaoxi.bms.service.role.BMSRoleMenuService;
import com.xiaoxi.bms.service.role.BMSRoleService;
import com.xiaoxi.bms.service.user.BMSUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author xiaoxi
 * @version 1.0
 * @date 2022/5/31 16:25
 */
@RestController
@RequestMapping("/role/api/v1")
@Slf4j
public class BMSRoleController {

    @Resource
    private BMSRoleService bmsRoleService;

    @Resource
    private BMSRoleMenuService bmsRoleMenuService;

    @Resource
    private BMSUserRoleService bmsUserRoleService;

    @OperationLog(operation = OperationConstants.CODE_QUERY_PAGE)
    @GetMapping("getAllRole")
    public ApiResponse getAllRole(BMSRoleDTO bmsRoleDTO, Pageable pageable) {

        IPage<BMSRole> bmsRoleIPage = bmsRoleService.getAllRole(bmsRoleDTO, pageable);

        return ApiResponse.ofSuccess(bmsRoleIPage);

    }

    @OperationLog(operation = OperationConstants.CODE_QUERY_ONE)
    @GetMapping("getRole/{roleId}")
    public ApiResponse getRoleByRoleId(@PathVariable Long roleId) {

        BMSRoleVO bmsRoleVO = null;
        try {
            bmsRoleVO = bmsRoleService.getRoleByRoleId(roleId);
        } catch (BMSException e) {
            log.error("未找到该角色:{}", e.getMessage());
            return ApiResponse.ofException(e);
        }

        return ApiResponse.ofSuccess(bmsRoleVO);

    }

    @OperationLog(operation = OperationConstants.CODE_MODIFY)
    @PostMapping("updateRole")
    public ApiResponse updateRole(@RequestBody BMSRoleVO bmsRoleVO) {

        try {

            if (StringUtils.isEmpty(bmsRoleVO.getName())) {
                return ApiResponse.ofEnum(RoleUserEnums.PLEASE_PASS_THE_ROLE_NAME);
            }

            if (StringUtils.isEmpty(bmsRoleVO.getIcon())) {
                return ApiResponse.ofEnum(RoleUserEnums.PLEASE_PASS_THE_ICON);
            }

            bmsRoleService.updateRole(bmsRoleVO);

        } catch (BMSException bmsException) {
            log.error("出现异常：{}", bmsException.getMessage());
            return ApiResponse.ofException(bmsException);
        }

        return ApiResponse.ofSuccess(bmsRoleVO);
    }

    @OperationLog(operation = OperationConstants.CODE_CREATE)
    @PostMapping("createRole")
    public ApiResponse createRole(@RequestBody BMSRoleVO bmsRoleVO) {

        try {

            if (StringUtils.isEmpty(bmsRoleVO.getName())) {
                return ApiResponse.ofEnum(RoleUserEnums.PLEASE_PASS_THE_ROLE_NAME);
            }

            if (StringUtils.isEmpty(bmsRoleVO.getIcon())) {
                return ApiResponse.ofEnum(RoleUserEnums.PLEASE_PASS_THE_ICON);
            }

            bmsRoleService.createRole(bmsRoleVO);

        } catch (BMSException bmsException) {
            log.error("出现异常：{} ", bmsException.getMessage());
            return ApiResponse.ofException(bmsException);
        }

        return ApiResponse.ofSuccess(bmsRoleVO);

    }

    @OperationLog(operation = OperationConstants.CODE_DELETE)
    @GetMapping("deleteRole/{roleId}")
    public ApiResponse deleteRole(@PathVariable Long roleId) {

        boolean deleteStatus = bmsRoleService.removeById(roleId);

        if (deleteStatus) {

            bmsRoleMenuService.deleteRoleMenuByRoleId(roleId);
            bmsUserRoleService.deleteUserRoleByRoleId(roleId);

            return ApiResponse.ofSuccess(roleId);
        } else {
            return ApiResponse.ofEnum(RoleUserEnums.INCORRECT_ROLE_ID);
        }

    }

    @OperationLog(operation = OperationConstants.CODE_CHECK)
    @GetMapping("checkRoleName")
    public ApiResponse checkRoleName(String roleName, Long id) {

        Boolean repeat = bmsRoleService.checkRoleName(roleName, id);
        if (repeat) {
            return ApiResponse.ofEnum(RoleUserEnums.ROLE_REGISTERED);
        }
        return ApiResponse.ofSuccess();

    }

}
